# Contributor: Borys Zhukov <mp5@mp5.im>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: omni <omni+alpine@hack.org>
pkgname=ocaml
pkgver=4.14.1
pkgrel=3
pkgdesc="Main implementation of the Caml programming language"
url="https://ocaml.org/"
arch="all !riscv64" # hangs forever at final build step
license="LGPL-2.1-or-later-WITH-linking-exception"
depends="$pkgname-runtime=$pkgver-r$pkgrel gcc"
makedepends="gdbm-dev libc-dev ncurses-dev zlib-dev binutils-dev"
checkdepends="parallel"
subpackages="
	$pkgname-doc
	$pkgname-dev
	$pkgname-ocamldoc
	$pkgname-compiler-libs:_compiler_libs
	$pkgname-runtime
	"
source="https://caml.inria.fr/pub/distrib/ocaml-${pkgver%.*}/ocaml-$pkgver.tar.gz"

# 32bit archs
case "$CARCH" in
armv7|armhf|x86)
	options="$options textrels"
	export LDFLAGS="$LDFLAGS -Wl,-z,notext"
	;;
esac

build() {
	./configure \
		--prefix /usr \
		--bindir /usr/bin \
		--libdir /usr/lib/ocaml \
		--mandir /usr/share/man \
		CC="${CC:-gcc}" \
		AS="${CC:-gcc} -c" \
		ASPP="${CC:-gcc} -c"
	make world.opt
}

check() {
	make ocamltest
	make -C testsuite parallel
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
	install -Dm644 Changes "$pkgdir"/usr/share/doc/$pkgname/Changes

	cd "$pkgdir"

	# Remove annotation files and sources.
	find usr/lib/ocaml \
		\( -name '*.cmt' -o -name '*.cmti' -o -name '*.ml' \) \
		-a -delete

	# To be consistent with other binaries.
	mv usr/bin/ocamldoc usr/bin/ocamldoc.byte
	ln -s ocamldoc.opt usr/bin/ocamldoc
}

dev() {
	pkgdesc="$pkgdesc (bytecode executables)"

	amove 'usr/bin/*.byte'
}

ocamldoc() {
	pkgdesc="$pkgdesc (documentation generator)"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname"  # for backward compatibility

	amove usr/bin/ocamldoc\*
	amove usr/lib/ocaml/ocamldoc
}

_compiler_libs() {
	pkgdesc="$pkgdesc (compiler libraries)"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname"  # for backward compatibility

	amove usr/lib/ocaml/compiler-libs
}

runtime() {
	pkgdesc="$pkgdesc (runtime environment)"
	depends=""
	replaces="$pkgname"  # for backward compatibility

	amove usr/bin/ocamlrun
	amove usr/lib/ocaml/ld.conf

	local i; for i in \
		'*.cmo' \
		'*.cmi' \
		'*.cma' \
		'stublibs' \
		'threads/*.cmi' \
		'threads/*.cma'
	do
		amove "usr/lib/ocaml/$i"
	done
}

sha512sums="
8fdeb1b370cb1f8a469a57966ac7dcb64814dd7c098a432c76bc61ba0f59d067e2b36aa29da9b86d048738db230dc5d813b99d577cdeeb4b1aa18ee563528f60  ocaml-4.14.1.tar.gz
"
